Page({
  data: {
    accessToken: "",
    isShow: false,
    results: [],
    src: "",
    isCamera: true,
    btnTxt: "拍照"
  },

  accessTokenFunc: function() {
    var that = this
    wx.cloud.callFunction({
      name: 'baiduAccessToken',
      success: res => {
        console.log(res)
        that.data.accessToken = res.result.access_token
        wx.setStorageSync("access_token", res.result.access_token)
        wx.setStorageSync("time", new Date().getTime())
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '调用失败',
        })
        console.error(err)
      }
    })
  },

  onLoad() {
    this.ctx = wx.createCameraContext()
    var time = wx.getStorageSync("time")
    var curTime = new Date().getTime()
    var timeNum = new Date(parseInt(curTime - time)).getDate()
    console.log("=======" + timeNum)
    var accessToken = wx.getStorageSync("access_token")
    console.log("====accessToken===" + accessToken)
    if (timeNum > 28 || (accessToken == "" ||
        accessToken == null || accessToken == undefined)) {
      this.accessTokenFunc()
    } else {
      this.setData({
        accessToken: wx.getStorageSync("access_token")
      })
    }
  },

  takePhoto() {
    var that = this
    if (this.data.isCamera == false) {
      this.setData({
        isCamera: true,
        btnTxt: "拍照"
      })
      return
    }
    this.ctx.takePhoto({
      quality: 'normal',
      success: (res) => {
        this.setData({
          src: res.tempImagePath,
          isCamera: false,
          btnTxt: "重拍"
        })
        wx.showLoading({
          title: '正在加载中',
        })
        wx.getFileSystemManager().readFile({
          filePath: res.tempImagePath,
          encoding: "base64",
          success: res => {
            console.log("图片读取成功")
            that.req(that.data.accessToken, res.data)
          },
          fail: res => {
            wx.hideLoading()
            wx.showToast({
              title: '拍照失败,未获取相机权限或其他原因',
              icon: "none"
            })
          }
        })
      }
    })
  },
  
  req: function(token, image) {
    var that = this
    wx.cloud.callFunction({
      name: 'license_plate',
      data: {
        "image": image,
        "token": token
      },
      success: res => {
        wx.hideLoading()
        let data = res.result
        console.log(JSON.stringify(res))
        if ("words_result" in data) {
          var results = res.result.words_result
          if (results != undefined && results != null) {
            that.setData({
              isShow: true,
              results: results
            })
            console.log(results)
          }
        }
        if ("error_code" in data) {
          wx.showToast({
            icon: 'none',
            title: 'AI识别失败,请重新拍照！',
          })
        }
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '调用失败',
        })
        console.error(err)
      }
    })
  },

  radioChange: function (e) {
    console.log(e.detail.value)
  },

  hideModal: function() {
    this.setData({
      isShow: false,
    })
  },
})